﻿// ***********************************************************
// 员工的数据访问类
// Creator:yehui  Date:2013-02-19
// ***********************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VipSys.SystemEntity;
using System.Data;
using VipSys.Common.Library;
using System.Diagnostics;

namespace VipSys.DataLayer
{
    public class LogAccess
    {
        private static LogAccess m_Instance = null;
        /// <summary>
        /// 单实例
        /// </summary>
        public static LogAccess Instance
        {
            get
            {
                if (m_Instance == null)
                    m_Instance = new LogAccess();
                return m_Instance;
            }
        }



        public short Del(string szID)
        {
            string szCondition = string.Format(" {0}={1}"
                , SystemData.LogTable.ID, szID);
            string szSql = string.Format("Delete from {0}  where {1}"
                , SystemData.DBTable.Log
                , szCondition);
            try
            {
                int result = SystemParam.Instance.DBAccess.ExecuteNonQuery(szSql);
                if (result <= 0)
                    return SystemData.ReturnValue.EXCEPTION;
                return SystemData.ReturnValue.OK;
            }
            catch (Exception ex)
            {
                StackTrace st = new StackTrace(new StackFrame(true));
                StackFrame sf = st.GetFrame(0);
                SystemParam.Instance.Log.Error(string.Format("{0}\nErrorInfo:{1}", sf.GetMethod(), ex.ToString()));
                return SystemData.ReturnValue.EXCEPTION;
            }
        }

        public short GetLogPageList(string szStart, string szLimit, ref List<LogInfo> lstLogInfo)
        {
            if (lstLogInfo == null)
                lstLogInfo = new List<LogInfo>();
            lstLogInfo.Clear();
            StringBuilder sbSql = new StringBuilder();
            sbSql.AppendFormat("SELECT TOP {0} * FROM {1}"
                , szLimit
                , SystemData.DBTable.Log
                );
            if (szStart != "0")
            {

                sbSql.Append(" WHERE (ID <");
                sbSql.AppendFormat(" (SELECT min(ID) FROM (SELECT TOP {0} id  FROM {1}  ORDER BY ID Desc) AS T)"
                    , szStart
                    , SystemData.DBTable.Log
                    );
                sbSql.Append(" )");
            }
            sbSql.Append(" ORDER BY  Date desc");
            try
            {
                DataSet ds = SystemParam.Instance.DBAccess.ExecuteQuery(sbSql.ToString());
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    LogInfo logInfo = new LogInfo();
                    logInfo.Level = ds.Tables[0].Rows[i][SystemData.LogTable.Level].ToString();
                    logInfo.ID = int.Parse(ds.Tables[0].Rows[i][SystemData.LogTable.ID].ToString());
                    logInfo.Date = ds.Tables[0].Rows[i][SystemData.LogTable.Date].ToString();
                    logInfo.Logger = ds.Tables[0].Rows[i][SystemData.LogTable.Logger].ToString();
                    logInfo.Message = ds.Tables[0].Rows[i][SystemData.LogTable.Message].ToString();
                    logInfo.Thread = ds.Tables[0].Rows[i][SystemData.LogTable.Message].ToString();
                    lstLogInfo.Add(logInfo);
                }
                return SystemData.ReturnValue.OK;
            }
            catch (Exception ex)
            {
                StackTrace st = new StackTrace(new StackFrame(true));
                StackFrame sf = st.GetFrame(0);
                SystemParam.Instance.Log.Error(string.Format("{0}\nErrorInfo:{1}", sf.GetMethod(), ex.ToString()));
                return SystemData.ReturnValue.EXCEPTION;
            }
        }
        public short GetTotalCount(ref int totalCount)
        {
            string szCondition = string.Format("1=1");
            string szSql = string.Format("select count(*) from {0} where {1}"
                , SystemData.DBTable.Log
                , szCondition);
            try
            {
                DataSet ds = SystemParam.Instance.DBAccess.ExecuteQuery(szSql);
                totalCount = int.Parse(ds.Tables[0].Rows[0][0].ToString());
                return SystemData.ReturnValue.OK;
            }
            catch (Exception ex)
            {
                StackTrace st = new StackTrace(new StackFrame(true));
                StackFrame sf = st.GetFrame(0);
                SystemParam.Instance.Log.Error(string.Format("{0}\nErrorInfo:{1}", sf.GetMethod(), ex.ToString()));
                return SystemData.ReturnValue.EXCEPTION;
            }
        }

        public short GetLog(string szID, ref LogInfo logInfo)
        {
            string szField = string.Format("{0},{1},[{2}],{3},{4},{5}"
                , SystemData.LogTable.Date
                , SystemData.LogTable.ID
                , SystemData.LogTable.Level
                , SystemData.LogTable.Logger
                , SystemData.LogTable.Message
                , SystemData.LogTable.Thread);
            string szCondition = string.Format("1=1");


            szCondition = string.Format("{0} and {1}={2}"
                , szCondition
                , SystemData.EmployeeTable.ID, szID.ToString());

            string szSql = string.Format("select {0} from {1} where {2}"
                , szField
                , SystemData.DBTable.Log
                , szCondition);
            try
            {
                DataSet ds = SystemParam.Instance.DBAccess.ExecuteQuery(szSql);
                if (ds == null || ds.Tables[0].Rows.Count <= 0)
                    return SystemData.ReturnValue.RES_NO_FOUND;
                logInfo.Level = ds.Tables[0].Rows[0][SystemData.LogTable.Level].ToString();
                logInfo.ID = int.Parse(ds.Tables[0].Rows[0][SystemData.LogTable.ID].ToString());
                logInfo.Date = ds.Tables[0].Rows[0][SystemData.LogTable.Date].ToString();
                logInfo.Logger = ds.Tables[0].Rows[0][SystemData.LogTable.Logger].ToString();
                logInfo.Message = ds.Tables[0].Rows[0][SystemData.LogTable.Message].ToString();
                logInfo.Thread = ds.Tables[0].Rows[0][SystemData.LogTable.Message].ToString();
                return SystemData.ReturnValue.OK;
            }
            catch (Exception ex)
            {
                StackTrace st = new StackTrace(new StackFrame(true));
                StackFrame sf = st.GetFrame(0);
                SystemParam.Instance.Log.Error(string.Format("{0}\nErrorInfo:{1}", sf.GetMethod(), ex.ToString()));
                return SystemData.ReturnValue.EXCEPTION;
            }
        }

    }
}
